﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using System.Reflection;

namespace AnyOffice.general.calendar.info
{
    public partial class _new :  ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
   
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DateTime time = (Request.QueryString["date"] != null) ? DateTime.Parse(Request.QueryString["date"].ToString()) : DateTime.Now;
                string userId = (Request.QueryString["user_id"] != null) ? Request.QueryString["user_id"].ToString() : "";
                this.ViewState["SpecifyDate"] = time.ToString("yyyy-MM-dd");
                this.ViewState["UserId"] = userId;
                if (!SecurityManager.IsCanManage(userId))
                {
                    base.CloseConn();
                    Response.Write(base.ShowErrorNoNav("无权限"));
                    Response.End();
                }
                this.litSpecifyDate.Text = time.Year.ToString() + "年" + time.Month.ToString() + "月" + time.Day.ToString() + "日";
                this.ckbMobile.Visible = SmsDao.IsMobile(SmsDao.SmsType.Calendar);
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                CalendarDao dao = new CalendarDao();
                CalendarData data = new CalendarData();
                data.CalType = 1;
                data.StartTime = DateTime.Parse(this.ViewState["SpecifyDate"].ToString() + " " + this.ddlStartHour.SelectedValue + ":" + this.ddlStartMinute.SelectedValue);
                data.EndTime = DateTime.Parse(this.ViewState["SpecifyDate"].ToString() + " " + this.ddlEndHour.SelectedValue + ":" + this.ddlEndMinute.SelectedValue);
                data.UserId = this.ViewState["UserId"].ToString();
                data.Content = this.txtContent.Value;
                data.ManagerId = base.User.Identity.Name;
                int num4 = dao.Insert(base.Conn, data);
                if (this.ckbSms.Checked || this.ckbMobile.Checked)
                {
                    SmsDao dao2 = new SmsDao();
                    SmsData data2 = new SmsData();
                    data2.FromId = base.User.Identity.Name;
                    data2.ToId = this.ViewState["UserId"].ToString();
                    data2.Type = 5;
                    data2.SendTime = DateTime.Parse(this.ViewState["SpecifyDate"].ToString() + " " + this.ddlStartHour.SelectedValue + ":" + this.ddlStartMinute.SelectedValue);
                    data2.Content = "请查看日程安排!\n内容:" + this.txtContent.Value;
                    data2.RelateId = num4;
                    dao2.BizSend(base.Conn, data2, this.ckbSms.Checked, this.ckbMobile.Checked);
                }
                this.litMessage.Text = base.ShowPromptClose("保存成功");
                DateTime time2 = new DateTime(2000, 1, 1);
                TimeSpan span = (TimeSpan)(Convert.ToDateTime(this.ViewState["SpecifyDate"].ToString()) - time2);
                StringBuilder builder = new StringBuilder();
                builder.Append("<script Language=\"JavaScript\">");
                builder.Append("window.opener.__doPostBack('DataCalendar1','" + span.Days.ToString() + "');");
                builder.Append("</script>");
                if (!this.Page.IsClientScriptBlockRegistered("refresh"))
                {
                    this.Page.RegisterClientScriptBlock("refresh", builder.ToString());
                }
            }
        } 

    }
}
